package com.capa.ui.home

import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.capa.BR
import com.capa.R
import com.capa.basic.BaseActivity
import com.capa.constants.Constants
import com.capa.databinding.ActivityHomeBinding
import com.capa.ui.chat.ChatFragment
import com.capa.ui.memo.MemoFragment
import com.capa.ui.video.VideoFragment
import com.capa.utils.DisplayUtil

class HomeActivity : BaseActivity<ActivityHomeBinding, HomeViewModel>() {
    private val fragments = ArrayList<Fragment>()
    private var lastBackPressedTime: Long = 0
    override fun initVariableId(): Int {
        return BR.model
    }

    override fun initContentView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_home
    }

    override fun initParam() {
        val paddingTop = binding?.bottomNav?.getItemPaddingTop()
        val paddingBottom = binding?.bottomNav?.getItemPaddingBottom()
        if (paddingBottom != null && paddingTop != null) {
            binding?.bottomNav?.setItemPaddingBottom(
                DisplayUtil.dp2px(
                    this,
                    (paddingBottom + paddingTop).toFloat() / 2
                )
            )

        }

        val statusBarHeight = getStatusBarHeight()
        binding?.apply {
            homeBar.layoutParams.height = statusBarHeight
        }

        fragments.apply {
            add(VideoFragment())
            add(MemoFragment())
            add(ChatFragment())
            add(MemoFragment())
        }
    }

    override fun initViewObservable() {
        // 初始化适配器
        binding?.vpContext?.let { viewPager ->
            viewPager.adapter = HomeAdapter(this, fragments).apply {
                // 预加载所有页面
                viewPager.offscreenPageLimit = fragments.size
            }

            // 注册页面变化监听
            viewPager.registerOnPageChangeCallback(
                object : ViewPager2.OnPageChangeCallback() {
                    override fun onPageSelected(position: Int) {
                        binding?.bottomNav?.selectedItemId = when(position) {
                            0 -> R.id.navigation_home
                            1 -> R.id.navigation_friend
                            2 -> R.id.navigation_message
                            3 -> R.id.navigation_me
                            else -> R.id.navigation_home
                        }
                    }
                }
            )
        }

        // HomeActivity.kt 中修改底部导航栏点击监听部分
        binding?.bottomNav?.setOnItemSelectedListener { item ->
            when (item.itemId) {
                R.id.add_vedio -> {
                    //TODO:发视频

                    //TODO:发视频后返回并更新好友视频列表（包含自己视频）
                    if(false){
                        binding?.vpContext?.currentItem = 1
                        binding?.bottomNav?.selectedItemId = R.id.navigation_friend
                    }
                    false
                }
                else -> {
                    // 其他菜单项正常切换页面
                    binding?.vpContext?.currentItem = when (item.itemId) {
                        R.id.navigation_home -> 0
                        R.id.navigation_friend -> 1
                        R.id.navigation_message -> 2
                        R.id.navigation_me -> 3
                        else -> 0
                    }
                    true
                }
            }
        }
    }

    override fun onBackPressed() {
        val currentTime = System.currentTimeMillis()
        if (currentTime - lastBackPressedTime <= 1000) {
            // 1000ms 内再次点击：直接退出
            finishAndRemoveTask() // 关闭当前 Activity
        } else {
            // 首次点击：提示用户
            Toast.makeText(this, "再按一次退出应用", Toast.LENGTH_SHORT).show()
            lastBackPressedTime = currentTime // 更新点击时间
        }
    }

    open fun getStatusBarHeight() : Int {
        var resourceId = getResources().getIdentifier(
            "status_bar_height", "dimen",
            "android"
        )

        Log.i(Constants.TAG, "getStatusBarHeight: resourceId = $resourceId")

        if (resourceId > 0) {
            var result = getResources().getDimensionPixelSize(resourceId)

            Log.i(Constants.TAG, "getStatusBarHeight: result = $result")
            return result; // 返回状态栏的高度
        }

        return 0
    }

}